$('#check-feed-dlg').dialog({
	autoOpen: false,
	title: 'Prüfe Feed',
	modal: true,
	resizable: false,
	draggable: false,
	width: 400,
	closeOnEscape: false,
	disabled: true,
	open: function(event, ui) {
		// Hide close button on this dialog
  		$(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
	},
});
$('#add-feed-form').dialog({
	autoOpen: false,
	title: 'Feed abonnieren',
	closeText: 'Schließen',
	modal: true,
	resizable: false,
	draggable: false,
	buttons: {
		"Abbrechen": function() { $(this).dialog("close"); },
		"OK": function() {
			$(this).dialog("close");
			var new_url = $('#addFeedURL')[0].value;
			if (new_url === "")
				return;
			$('#check-feed-dlg').dialog('open');
			$.ajax({
				url: "{% url feedmanager_add_feed %}",
				type: 'POST',
				data: ({'url': new_url}), 
				cache: false,
				success: function(response) {
					$('#check-feed-dlg').dialog('close');
					if (response !== null && response !== "") {
						showDialogMessage(response.data);
						if (is_dashboard && typeof reload_dashboard == 'function')
							reload_dashboard();
						else
							if (!is_dashboard && typeof reload_errorlog == 'function')
								if (errorlog_page)
									reload_errorlog(errorlog_page);
								else
									reload_errorlog("");
					}
				},
				error: function(response) {
					$('#check-feed-dlg').dialog('close');
					if (response !== null && response !== "")
						showDialogMessage(response.data);
				},
			});
		},
	},
	open: function(event, ui) {
		var container = $('#addFeedURL'); 
		container.focus().val('');
	},
	width: 400,
});
function showDialogMessage(text) {
	$('<div></div>')
		.html($.trim(text))
		.dialog({
			closeText: 'Schließen',
			modal: true,
			resizable: false,
			draggable: false,
			buttons: {"OK": function() { $(this).dialog("close"); },},
			title: 'Hinweis',
			width: 400,
		});
}

/////////////////////////////////////////////////////////////////
// Django's Cross Site Request Forgery protection with jQuery  // 
// http://docs.djangoproject.com/en/1.2/ref/contrib/csrf/#ajax //
/////////////////////////////////////////////////////////////////
$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});
/////////////////////////////////////////////////////////////////
